<?php

namespace App\Listeners;

use App\Events\OrderAppointed;
use App\Events\OrderComplained;
use App\Events\OrderEmployed;
use App\Models\MasterStatistic;
use App\Models\Order;
use App\Models\OrderComplain;
use App\Notifications\OrderNotifications\OrderAppointedNotification;
use App\Notifications\OrderNotifications\OrderComplainedNotification;
use App\Notifications\OrderNotifications\OrderEmployedNotification;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class OrderComplainedListener implements ShouldQueue
{

    use InteractsWithQueue;

    public $queue = 'order';

    public $tries = 3;

    public $timeout = 30;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  CacheHit  $event
     * @return void
     */
    public function handle(OrderComplained $event)
    {
        $complain = OrderComplain::find($event->complain_id);
        if(!$complain){
            throw new \Exception('miss complain');
        }

        $complain->merchant->notify(new OrderComplainedNotification($complain));
        $complain->order->master->notify(new OrderComplainedNotification($complain));
    }

    public function failed(OrderComplained $event, \Exception $exception)
    {
        //
    }
}
